package net.roseboy.classfinal;


import a.a.a.A;
import a.a.a.B;
import net.roseboy.classfinal.util.*;

import java.io.*;
import java.lang.instrument.Instrumentation;


/**
 * 监听类加载
 *
 * @author roseboy
 */
public class CoreAgent {

    public static byte[] password={2,1,3,2,1,4,34,88,2,12,1,2,12,1,3,2,3,2,3,1,2,1,21,77,2,3,66,3,2,3,1,2,1,3,44,43,3};
    public static byte[] cp={3,4,2,57,3};

    /**
     * man方法执行前调用
     *
     * @param args 参数
     * @param inst inst
     */
    public static void premain(String args, Instrumentation inst) {



        Const.pringInfo();
        initB();
        if (inst != null) {
            inst.addTransformer(new AgentTransformer());
        }
    }

    public static void initB() {

        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        for (int i = 'A'; i <= 'M'; i++) {
            String path="/a/a/a/"+(char)i+".class";
            InputStream in = A.class.getResourceAsStream(path);
            if(in!=null){
                try {
                    IoUtils.copy(in, bos);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        byte[] bs=bos.toByteArray();
        for (int i = 0; i < bs.length; i++) {
            bs[i]=(byte)(bs[i]^password[i%password.length]);
            bs[i]=(byte)(bs[i]^cp[i%cp.length]);
        }
        B.i(bos.toByteArray());
    }
}
